package csel.view;

import java.awt.Container;

import javax.swing.JFrame;

import csel.model.*;

/**
 * Provides an interface for the display of the currently viewable area. 
 * @author david
 */
public class AreaViewport extends Viewport
{
	final Model model;
	final TrampolineListener listener;
	final JFrame frame;
	final AreaViewPanel panel;

	public AreaViewport(Model model)
	{
		this.model = model;
		
		listener = new TrampolineListener();
		
		frame = new JFrame("CSEL Map");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setFocusable(true);
		frame.addKeyListener(listener);
		
		panel = new AreaViewPanel(model);
		frame.add(panel);
		frame.pack();
		frame.setResizable(false);
	}
	
	@Override
	public void close()
	{
		panel.stopRenderer();
		frame.dispose();
		System.out.println("[VIEW] AreaViewport disposed.");
	}

	@Override
	public void setVisible(boolean visibility)
	{
		/* not really the right place to put it but... */
		if (visibility && !frame.isVisible())
			panel.startLoop();
		
		frame.setVisible(visibility);
	}

	@Override
	protected Container getContainer()
	{
		return frame;
	}
}
